<?php 
if(isset($_POST['savecat']))
{
	$catname = trim($_POST['name']);
	$num = mysql_num_rows(mysql_query("select * from category where Name='$catname'"));
	if($num==0)
	{
		mysql_query("insert into category (Name) values ('$catname')");	
		//header("location: index.php?tc=addcat&_s=1");
		echo "<script>window.location='index.php?tc=addcat&_s=1'</script>";
	}
	else
	{
		//header("location: index.php?tc=addcat&_e=1");
		echo "<script>window.location='index.php?tc=addcat&_e=1'</script>";
	}
}
?>

<script type="text/javascript">
function validate()
{
	if(document.up.name.value=="")
	{
		alert("Please enter categoty name.");	
		document.up.name.focus();
		return false;
	}
}
</script>
<?php if($_REQUEST['_s'] == 1){ ?>
    <div class="sucmsg"><img src="images/information.png" align="absmiddle" />&nbsp;Successfully saved.</div>
<?php } ?>
<?php if($_REQUEST['_e'] == 1){ ?>
    <div class="sucmsg"><img src="images/error.png" align="absmiddle" />&nbsp;Category name already exists.</div>
<?php } ?>

<form name="up" action="index.php?tc=addcat&_a=a" method="post" onSubmit="return validate()">
<table width="99%" align="center" border="0"  class="list" cellpadding="5">
<thead>
<tr>
	<td colspan="2" align="left">
        <div style="float:left">Category</div>
        <div style="float:right"><a href="index.php?tc=cat">Back</a></div>
    </td>
</tr>
</thead>
<tr>
	<td align="right" width="150">Category Name</td>
    <td align="left"><input type="text" name="name" style="width:200px;"  /></td>
</tr>
<tr>
	<td colspan="2" style="padding-left:155px;">
    	<input type="submit" value="Save" name="savecat" >&nbsp;&nbsp;
        
    </td>
</tr>        
</table>   
</form> 
